/* 代码 */
export function getAllLastChild(list, lastchildren) {
  // console.log(list)
  return list.reduce((res, item) => {
    if(!item.hasOwnProperty('children')) res.push(item)
    let lastChild = []
    if(item.hasOwnProperty('children')) {
      lastChild = getAllLastChild(item.children)
    }
    return [...res, ...lastChild]
  }, [])
}

// 筛选state 为 5 的数据
export function filterNodeXX(arr) {
  return arr.reduce((result, item) => {
    if (item.state === 5) result.push(item);
    let deepChildren = [];
    if (item.hasOwnProperty('children')) {
      deepChildren = filterNodeXX(item.children);
    }
    return [...result, ...deepChildren];
  }, [])
}

export function filterNode(arr) {
  const filNode = arr.filter(item => {
    if(item.state === 5) {
      // console.log(item)
      return item
    } else {
      if(item.hasOwnProperty('children')) {
        item.children = filterNode(item.children)
      } else {
        return false
      }
      return item
    }
  })
  return filNode
}

export function changeDataTree(arr) {
  if(!arr) {
    return false
  }
  for(let v of arr) {
    if(v.hasOwnProperty('show')) {
      // console.log(v)
      v['_children'] = v.children
      v.children = null
    }
    if(v.children) {
      changeDataTree(v.children)
    }
  }
}

export function treeToOneArr(arr) {
  const arrC = arr.map(item => {
    // item['name'] = item['Company Name'].length > 18 ?
    //   item['Company Name'].substr(0,16) + '...' :
    //   item['Company Name']
    if(item.hasOwnProperty('children')) {
      item.children = treeToOneArr(item.children)
    } else {
      return item
    }
    return item
  })
  return arrC
}

export function getChildNodes(node) {
  let nodes = node.children
  let arr = []
  for(let i = 0; i < nodes.length; i++) {
    var childNode = nodes[i]
    if(childNode.hasOwnProperty('children')) {
      arr.push(childNode)
      var temp = getChildNodes(childNode)
      arr.concat(temp)
    }
  }
  return arr
}